{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Binning Data with Pandas cut and qcut\n",
    "\n",
    "This notebook accompanies the article posted on [pbpython.com](http://pbpython.com/pandas-qcut-cut.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style('whitegrid')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_df = pd.read_excel('https://github.com/chris1610/pbpython/blob/master/data/2018_Sales_Total_v2.xlsx?raw=true')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = raw_df.groupby(['account number', 'name'])['ext price'].sum().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account number</th>\n",
       "      <th>name</th>\n",
       "      <th>ext price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>141962</td>\n",
       "      <td>Herman LLC</td>\n",
       "      <td>63626.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>146832</td>\n",
       "      <td>Kiehn-Spinka</td>\n",
       "      <td>99608.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>163416</td>\n",
       "      <td>Purdy-Kunde</td>\n",
       "      <td>77898.21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>218895</td>\n",
       "      <td>Kulas Inc</td>\n",
       "      <td>137351.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>239344</td>\n",
       "      <td>Stokes LLC</td>\n",
       "      <td>91535.92</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   account number          name  ext price\n",
       "0          141962    Herman LLC   63626.03\n",
       "1          146832  Kiehn-Spinka   99608.77\n",
       "2          163416   Purdy-Kunde   77898.21\n",
       "3          218895     Kulas Inc  137351.96\n",
       "4          239344    Stokes LLC   91535.92"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A histogram is an example of binning data and showing the visual representation of the data distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introducing qcut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7f61c1ebae80>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD3CAYAAAAJxX+sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAUbUlEQVR4nO3df0xV9/3H8deVHxcFlL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e8TtsT/00EM6dOiQcnNzZYxRWVmZhg4dqhUrVmjNmjXKyMjQxIkTFRUVpaKiIhUWFsoYo4ULF8rtdqugoEDFxcUqKChQTEyMqqqqJKnzsI7P51NOTo6++93vOpJ/5syZKikpUWFhoTo6OrRw4ULdf//9EZNfkoqLix3LO3r0aE2fPl1+v19lZWUhze3z+VReXq4hQ4Zo3rx5kqTvfe97mj9/fp/P3p3BgwdHRPbi4mKVlpZq+/btSkhIUFVVlQYNGhQR2VetWqWFCxcqOjpaMTExeumll/r8dueSAgBgmYg7FAMA6BnFDgCWodgBwDIUOwBYhmIHAMtQ7ABgGYodACzzfxcvBS+/c6VNAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df['ext price'].plot(kind='hist')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Describe shows how data can be cut by percentiles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count        20.000000\n",
       "mean     101711.287500\n",
       "std       27037.449673\n",
       "min       55733.050000\n",
       "25%       89137.707500\n",
       "50%      100271.535000\n",
       "75%      110132.552500\n",
       "max      184793.700000\n",
       "Name: ext price, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['ext price'].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is an example of using [qcut](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.qcut.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     (55733.049000000006, 89137.708]\n",
       "1             (89137.708, 100271.535]\n",
       "2     (55733.049000000006, 89137.708]\n",
       "3              (110132.552, 184793.7]\n",
       "4             (89137.708, 100271.535]\n",
       "5             (89137.708, 100271.535]\n",
       "6     (55733.049000000006, 89137.708]\n",
       "7            (100271.535, 110132.552]\n",
       "8              (110132.552, 184793.7]\n",
       "9              (110132.552, 184793.7]\n",
       "10            (89137.708, 100271.535]\n",
       "11    (55733.049000000006, 89137.708]\n",
       "12    (55733.049000000006, 89137.708]\n",
       "13            (89137.708, 100271.535]\n",
       "14           (100271.535, 110132.552]\n",
       "15             (110132.552, 184793.7]\n",
       "16           (100271.535, 110132.552]\n",
       "17             (110132.552, 184793.7]\n",
       "18           (100271.535, 110132.552]\n",
       "19           (100271.535, 110132.552]\n",
       "Name: ext price, dtype: category\n",
       "Categories (4, interval[float64]): [(55733.049000000006, 89137.708] < (89137.708, 100271.535] < (100271.535, 110132.552] < (110132.552, 184793.7]]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.qcut(df['ext price'], q=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assign the results of the values back to the original dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['quantile_ex_1'] = pd.qcut(df['ext price'], q=4)\n",
    "df['quantile_ex_2'] = pd.qcut(df['ext price'], q=10, precision=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account number</th>\n",
       "      <th>name</th>\n",
       "      <th>ext price</th>\n",
       "      <th>quantile_ex_1</th>\n",
       "      <th>quantile_ex_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>141962</td>\n",
       "      <td>Herman LLC</td>\n",
       "      <td>63626.03</td>\n",
       "      <td>(55733.049000000006, 89137.708]</td>\n",
       "      <td>(55732.0, 76471.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>146832</td>\n",
       "      <td>Kiehn-Spinka</td>\n",
       "      <td>99608.77</td>\n",
       "      <td>(89137.708, 100271.535]</td>\n",
       "      <td>(95908.0, 100272.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>163416</td>\n",
       "      <td>Purdy-Kunde</td>\n",
       "      <td>77898.21</td>\n",
       "      <td>(55733.049000000006, 89137.708]</td>\n",
       "      <td>(76471.0, 87168.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>218895</td>\n",
       "      <td>Kulas Inc</td>\n",
       "      <td>137351.96</td>\n",
       "      <td>(110132.552, 184793.7]</td>\n",
       "      <td>(124778.0, 184794.0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>239344</td>\n",
       "      <td>Stokes LLC</td>\n",
       "      <td>91535.92</td>\n",
       "      <td>(89137.708, 100271.535]</td>\n",
       "      <td>(90686.0, 95908.0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   account number          name  ext price                    quantile_ex_1  \\\n",
       "0          141962    Herman LLC   63626.03  (55733.049000000006, 89137.708]   \n",
       "1          146832  Kiehn-Spinka   99608.77          (89137.708, 100271.535]   \n",
       "2          163416   Purdy-Kunde   77898.21  (55733.049000000006, 89137.708]   \n",
       "3          218895     Kulas Inc  137351.96           (110132.552, 184793.7]   \n",
       "4          239344    Stokes LLC   91535.92          (89137.708, 100271.535]   \n",
       "\n",
       "          quantile_ex_2  \n",
       "0    (55732.0, 76471.0]  \n",
       "1   (95908.0, 100272.0]  \n",
       "2    (76471.0, 87168.0]  \n",
       "3  (124778.0, 184794.0]  \n",
       "4    (90686.0, 95908.0]  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Look at the distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(110132.552, 184793.7]             5\n",
       "(100271.535, 110132.552]           5\n",
       "(89137.708, 100271.535]            5\n",
       "(55733.049000000006, 89137.708]    5\n",
       "Name: quantile_ex_1, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['quantile_ex_1'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(124778.0, 184794.0]    2\n",
       "(112290.0, 124778.0]    2\n",
       "(105938.0, 112290.0]    2\n",
       "(103606.0, 105938.0]    2\n",
       "(100272.0, 103606.0]    2\n",
       "(95908.0, 100272.0]     2\n",
       "(90686.0, 95908.0]      2\n",
       "(87168.0, 90686.0]      2\n",
       "(76471.0, 87168.0]      2\n",
       "(55732.0, 76471.0]      2\n",
       "Name: quantile_ex_2, dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['quantile_ex_2'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account number</th>\n",
       "      <th>name</th>\n",
       "      <th>ext price</th>\n",
       "      <th>quantile_ex_1</th>\n",
       "      <th>quantile_ex_2</th>\n",
       "      <th>quantile_ex_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>141962</td>\n",
       "      <td>Herman LLC</td>\n",
       "      <td>63626.03</td>\n",
       "      <td>(55733.049000000006, 89137.708]</td>\n",
       "      <td>(55732.0, 76471.0]</td>\n",
       "      <td>Bronze</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>146832</td>\n",
       "      <td>Kiehn-Spinka</td>\n",
       "      <td>99608.77</td>\n",
       "      <td>(89137.708, 100271.535]</td>\n",
       "      <td>(95908.0, 100272.0]</td>\n",
       "      <td>Gold</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>163416</td>\n",
       "      <td>Purdy-Kunde</td>\n",
       "      <td>77898.21</td>\n",
       "      <td>(55733.049000000006, 89137.708]</td>\n",
       "      <td>(76471.0, 87168.0]</td>\n",
       "      <td>Bronze</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>218895</td>\n",
       "      <td>Kulas Inc</td>\n",
       "      <td>137351.96</td>\n",
       "      <td>(110132.552, 184793.7]</td>\n",
       "      <td>(124778.0, 184794.0]</td>\n",
       "      <td>Diamond</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>239344</td>\n",
       "      <td>Stokes LLC</td>\n",
       "      <td>91535.92</td>\n",
       "      <td>(89137.708, 100271.535]</td>\n",
       "      <td>(90686.0, 95908.0]</td>\n",
       "      <td>Silver</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   account number          name  ext price                    quantile_ex_1  \\\n",
       "0          141962    Herman LLC   63626.03  (55733.049000000006, 89137.708]   \n",
       "1          146832  Kiehn-Spinka   99608.77          (89137.708, 100271.535]   \n",
       "2          163416   Purdy-Kunde   77898.21  (55733.049000000006, 89137.708]   \n",
       "3          218895     Kulas Inc  137351.96           (110132.552, 184793.7]   \n",
       "4          239344    Stokes LLC   91535.92          (89137.708, 100271.535]   \n",
       "\n",
       "          quantile_ex_2 quantile_ex_3  \n",
       "0    (55732.0, 76471.0]        Bronze  \n",
       "1   (95908.0, 100272.0]          Gold  \n",
       "2    (76471.0, 87168.0]        Bronze  \n",
       "3  (124778.0, 184794.0]       Diamond  \n",
       "4    (90686.0, 95908.0]        Silver  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin_labels_5 = ['Bronze', 'Silver', 'Gold', 'Platinum', 'Diamond']\n",
    "df['quantile_ex_3'] = pd.qcut(df['ext price'],\n",
    "                              q=[0, .2, .4, .6, .8, 1],\n",
    "                              labels=bin_labels_5)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Diamond     4\n",
       "Platinum    4\n",
       "Gold        4\n",
       "Silver      4\n",
       "Bronze      4\n",
       "Name: quantile_ex_3, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['quantile_ex_3'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "results, bin_edges = pd.qcut(df['ext price'],\n",
    "                             q=[0, .2, .4, .6, .8, 1],\n",
    "                             labels=bin_labels_5,\n",
    "                             retbins=True)\n",
    "\n",
    "results_table = pd.DataFrame(zip(bin_edges, bin_labels_5),\n",
    "                             columns=['Threshold', 'Tier'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Threshold</th>\n",
       "      <th>Tier</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>55733.050</td>\n",
       "      <td>Bronze</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>87167.958</td>\n",
       "      <td>Silver</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>95908.156</td>\n",
       "      <td>Gold</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>103605.970</td>\n",
       "      <td>Platinum</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>112290.054</td>\n",
       "      <td>Diamond</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Threshold      Tier\n",
       "0   55733.050    Bronze\n",
       "1   87167.958    Silver\n",
       "2   95908.156      Gold\n",
       "3  103605.970  Platinum\n",
       "4  112290.054   Diamond"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>quantile_ex_1</th>\n",
       "      <th>quantile_ex_2</th>\n",
       "      <th>quantile_ex_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>count</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>unique</td>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>top</td>\n",
       "      <td>(110132.552, 184793.7]</td>\n",
       "      <td>(124778.0, 184794.0]</td>\n",
       "      <td>Diamond</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>freq</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 quantile_ex_1         quantile_ex_2 quantile_ex_3\n",
       "count                       20                    20            20\n",
       "unique                       4                    10             5\n",
       "top     (110132.552, 184793.7]  (124778.0, 184794.0]       Diamond\n",
       "freq                         5                     2             4"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe(include='category')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can pass the percentiles to use to describe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account number</th>\n",
       "      <th>ext price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>count</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>20.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>mean</td>\n",
       "      <td>476998.750000</td>\n",
       "      <td>101711.287500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>std</td>\n",
       "      <td>231499.208970</td>\n",
       "      <td>27037.449673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>min</td>\n",
       "      <td>141962.000000</td>\n",
       "      <td>55733.050000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0%</td>\n",
       "      <td>141962.000000</td>\n",
       "      <td>55733.050000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>33.3%</td>\n",
       "      <td>332759.333333</td>\n",
       "      <td>91241.493333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50%</td>\n",
       "      <td>476006.500000</td>\n",
       "      <td>100271.535000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>66.7%</td>\n",
       "      <td>662511.000000</td>\n",
       "      <td>104178.580000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>100%</td>\n",
       "      <td>786968.000000</td>\n",
       "      <td>184793.700000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>max</td>\n",
       "      <td>786968.000000</td>\n",
       "      <td>184793.700000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       account number      ext price\n",
       "count       20.000000      20.000000\n",
       "mean    476998.750000  101711.287500\n",
       "std     231499.208970   27037.449673\n",
       "min     141962.000000   55733.050000\n",
       "0%      141962.000000   55733.050000\n",
       "33.3%   332759.333333   91241.493333\n",
       "50%     476006.500000  100271.535000\n",
       "66.7%   662511.000000  104178.580000\n",
       "100%    786968.000000  184793.700000\n",
       "max     786968.000000  184793.700000"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe(percentiles=[0, 1/3, 2/3, 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "labels=False will return integers for each bin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account number</th>\n",
       "      <th>name</th>\n",
       "      <th>ext price</th>\n",
       "      <th>quantile_ex_1</th>\n",
       "      <th>quantile_ex_2</th>\n",
       "      <th>quantile_ex_3</th>\n",
       "      <th>quantile_ex_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>141962</td>\n",
       "      <td>Herman LLC</td>\n",
       "      <td>63626.03</td>\n",
       "      <td>(55733.049000000006, 89137.708]</td>\n",
       "      <td>(55732.0, 76471.0]</td>\n",
       "      <td>Bronze</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>146832</td>\n",
       "      <td>Kiehn-Spinka</td>\n",
       "      <td>99608.77</td>\n",
       "      <td>(89137.708, 100271.535]</td>\n",
       "      <td>(95908.0, 100272.0]</td>\n",
       "      <td>Gold</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>163416</td>\n",
       "      <td>Purdy-Kunde</td>\n",
       "      <td>77898.21</td>\n",
       "      <td>(55733.049000000006, 89137.708]</td>\n",
       "      <td>(76471.0, 87168.0]</td>\n",
       "      <td>Bronze</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>218895</td>\n",
       "      <td>Kulas Inc</td>\n",
       "      <td>137351.96</td>\n",
       "      <td>(110132.552, 184793.7]</td>\n",
       "      <td>(124778.0, 184794.0]</td>\n",
       "      <td>Diamond</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>239344</td>\n",
       "      <td>Stokes LLC</td>\n",
       "      <td>91535.92</td>\n",
       "      <td>(89137.708, 100271.535]</td>\n",
       "      <td>(90686.0, 95908.0]</td>\n",
       "      <td>Silver</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   account number          name  ext price                    quantile_ex_1  \\\n",
       "0          141962    Herman LLC   63626.03  (55733.049000000006, 89137.708]   \n",
       "1          146832  Kiehn-Spinka   99608.77          (89137.708, 100271.535]   \n",
       "2          163416   Purdy-Kunde   77898.21  (55733.049000000006, 89137.708]   \n",
       "3          218895     Kulas Inc  137351.96           (110132.552, 184793.7]   \n",
       "4          239344    Stokes LLC   91535.92          (89137.708, 100271.535]   \n",
       "\n",
       "          quantile_ex_2 quantile_ex_3  quantile_ex_4  \n",
       "0    (55732.0, 76471.0]        Bronze              0  \n",
       "1   (95908.0, 100272.0]          Gold              2  \n",
       "2    (76471.0, 87168.0]        Bronze              0  \n",
       "3  (124778.0, 184794.0]       Diamond              4  \n",
       "4    (90686.0, 95908.0]        Silver              1  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['quantile_ex_4'] = pd.qcut(df['ext price'],\n",
    "                            q=[0, .2, .4, .6, .8, 1],\n",
    "                            labels=False)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## cut"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remove the added columns to make the examples shorter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.drop(columns = ['quantile_ex_1','quantile_ex_2', 'quantile_ex_3', 'quantile_ex_4'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       (55603.989, 87998.212]\n",
       "1      (87998.212, 120263.375]\n",
       "2       (55603.989, 87998.212]\n",
       "3     (120263.375, 152528.538]\n",
       "4      (87998.212, 120263.375]\n",
       "5      (87998.212, 120263.375]\n",
       "6       (55603.989, 87998.212]\n",
       "7      (87998.212, 120263.375]\n",
       "8      (87998.212, 120263.375]\n",
       "9       (152528.538, 184793.7]\n",
       "10     (87998.212, 120263.375]\n",
       "11      (55603.989, 87998.212]\n",
       "12      (55603.989, 87998.212]\n",
       "13     (87998.212, 120263.375]\n",
       "14     (87998.212, 120263.375]\n",
       "15    (120263.375, 152528.538]\n",
       "16     (87998.212, 120263.375]\n",
       "17     (87998.212, 120263.375]\n",
       "18     (87998.212, 120263.375]\n",
       "19     (87998.212, 120263.375]\n",
       "Name: ext price, dtype: category\n",
       "Categories (4, interval[float64]): [(55603.989, 87998.212] < (87998.212, 120263.375] < (120263.375, 152528.538] < (152528.538, 184793.7]]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.cut(df['ext price'], bins=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(87998.212, 120263.375]     12\n",
       "(55603.989, 87998.212]       5\n",
       "(120263.375, 152528.538]     2\n",
       "(152528.538, 184793.7]       1\n",
       "Name: ext price, dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.cut(df['ext price'], bins=4).value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "cut_labels_4 = ['silver', 'gold', 'platinum', 'diamond']\n",
    "cut_bins = [0, 70000, 100000, 130000, 200000]\n",
    "df['cut_ex1'] = pd.cut(df['ext price'], bins=cut_bins, labels=cut_labels_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account number</th>\n",
       "      <th>name</th>\n",
       "      <th>ext price</th>\n",
       "      <th>cut_ex1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>141962</td>\n",
       "      <td>Herman LLC</td>\n",
       "      <td>63626.03</td>\n",
       "      <td>silver</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>146832</td>\n",
       "      <td>Kiehn-Spinka</td>\n",
       "      <td>99608.77</td>\n",
       "      <td>gold</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>163416</td>\n",
       "      <td>Purdy-Kunde</td>\n",
       "      <td>77898.21</td>\n",
       "      <td>gold</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>218895</td>\n",
       "      <td>Kulas Inc</td>\n",
       "      <td>137351.96</td>\n",
       "      <td>diamond</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>239344</td>\n",
       "      <td>Stokes LLC</td>\n",
       "      <td>91535.92</td>\n",
       "      <td>gold</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   account number          name  ext price  cut_ex1\n",
       "0          141962    Herman LLC   63626.03   silver\n",
       "1          146832  Kiehn-Spinka   99608.77     gold\n",
       "2          163416   Purdy-Kunde   77898.21     gold\n",
       "3          218895     Kulas Inc  137351.96  diamond\n",
       "4          239344    Stokes LLC   91535.92     gold"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use nump.linspace to define the ranges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([     0.,  25000.,  50000.,  75000., 100000., 125000., 150000.,\n",
       "       175000., 200000.])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0, 200000, 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       (50000.0, 75000.0]\n",
       "1      (75000.0, 100000.0]\n",
       "2      (75000.0, 100000.0]\n",
       "3     (125000.0, 150000.0]\n",
       "4      (75000.0, 100000.0]\n",
       "5      (75000.0, 100000.0]\n",
       "6      (75000.0, 100000.0]\n",
       "7     (100000.0, 125000.0]\n",
       "8     (100000.0, 125000.0]\n",
       "9     (175000.0, 200000.0]\n",
       "10     (75000.0, 100000.0]\n",
       "11      (50000.0, 75000.0]\n",
       "12     (75000.0, 100000.0]\n",
       "13     (75000.0, 100000.0]\n",
       "14    (100000.0, 125000.0]\n",
       "15    (100000.0, 125000.0]\n",
       "16    (100000.0, 125000.0]\n",
       "17    (100000.0, 125000.0]\n",
       "18    (100000.0, 125000.0]\n",
       "19    (100000.0, 125000.0]\n",
       "Name: ext price, dtype: category\n",
       "Categories (8, interval[float64]): [(0.0, 25000.0] < (25000.0, 50000.0] < (50000.0, 75000.0] < (75000.0, 100000.0] < (100000.0, 125000.0] < (125000.0, 150000.0] < (150000.0, 175000.0] < (175000.0, 200000.0]]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.cut(df['ext price'], bins=np.linspace(0, 200000, 9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy arange is another option"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([     0,  10000,  20000,  30000,  40000,  50000,  60000,  70000,\n",
       "        80000,  90000, 100000, 110000, 120000, 130000, 140000, 150000,\n",
       "       160000, 170000, 180000, 190000])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0, 200000, 10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "IntervalIndex([[0, 10000), [10000, 20000), [20000, 30000), [30000, 40000), [40000, 50000) ... [150000, 160000), [160000, 170000), [170000, 180000), [180000, 190000), [190000, 200000)],\n",
       "              closed='left',\n",
       "              dtype='interval[int64]')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.interval_range(start=0, freq=10000, end=200000, closed='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account number</th>\n",
       "      <th>name</th>\n",
       "      <th>ext price</th>\n",
       "      <th>cut_ex1</th>\n",
       "      <th>cut_ex2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>141962</td>\n",
       "      <td>Herman LLC</td>\n",
       "      <td>63626.03</td>\n",
       "      <td>silver</td>\n",
       "      <td>(60000, 70000]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>146832</td>\n",
       "      <td>Kiehn-Spinka</td>\n",
       "      <td>99608.77</td>\n",
       "      <td>gold</td>\n",
       "      <td>(90000, 100000]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>163416</td>\n",
       "      <td>Purdy-Kunde</td>\n",
       "      <td>77898.21</td>\n",
       "      <td>gold</td>\n",
       "      <td>(70000, 80000]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>218895</td>\n",
       "      <td>Kulas Inc</td>\n",
       "      <td>137351.96</td>\n",
       "      <td>diamond</td>\n",
       "      <td>(130000, 140000]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>239344</td>\n",
       "      <td>Stokes LLC</td>\n",
       "      <td>91535.92</td>\n",
       "      <td>gold</td>\n",
       "      <td>(90000, 100000]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   account number          name  ext price  cut_ex1           cut_ex2\n",
       "0          141962    Herman LLC   63626.03   silver    (60000, 70000]\n",
       "1          146832  Kiehn-Spinka   99608.77     gold   (90000, 100000]\n",
       "2          163416   Purdy-Kunde   77898.21     gold    (70000, 80000]\n",
       "3          218895     Kulas Inc  137351.96  diamond  (130000, 140000]\n",
       "4          239344    Stokes LLC   91535.92     gold   (90000, 100000]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interval_range = pd.interval_range(start=0, freq=10000, end=200000)\n",
    "df['cut_ex2'] = pd.cut(df['ext price'], bins=interval_range, labels=[1,2,3])\n",
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
